import os
import sys

# BASE_PATH is the root directory of the tool and needs to be added to sys.path.
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_PATH)

from utils import Utils
from common.logger import log
from common.benchmark_var import GlobalEnv, GlobalVar, ValidateOptions


def add_file_hash(res_file_name):
    with open(os.path.join(GlobalEnv.GCBS_UTILS_DIR, res_file_name), "w") as file:
        for file_name in Utils.get_files_to_be_verified():
            res = Utils.md5_file(file_name)
            if not res:
                log.error("Hash file:{} failed!".format(file_name))
                return False
            file.write("".join([file_name.split(BASE_PATH)[1], ":", res, "\n"]))
    return True


if __name__ == '__main__':
    if not add_file_hash(ValidateOptions.HASH_FILE):
        log.error("Get file hash failed!")
